<template><div><h1 id="解决js精度丢失" tabindex="-1"><a class="header-anchor" href="#解决js精度丢失" aria-hidden="true">#</a> 解决js精度丢失</h1>
<h2 id="一、使用" tabindex="-1"><a class="header-anchor" href="#一、使用" aria-hidden="true">#</a> 一、使用</h2>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code>npm i bignumber<span class="token punctuation">.</span>js

<span class="token comment">// 使用</span>
<span class="token keyword">import</span> BigNumber <span class="token keyword">from</span> <span class="token string">'bignumber.js'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="二、文档" tabindex="-1"><a class="header-anchor" href="#二、文档" aria-hidden="true">#</a> 二、文档</h2>
<p><a href="https://gitee.com/web/bignumber.js/" target="_blank" rel="noopener noreferrer">官方文档<ExternalLinkIcon/></a>
<a href="https://www.jianshu.com/p/f5d3e379744c" target="_blank" rel="noopener noreferrer">中文文档<ExternalLinkIcon/></a></p>
<h3 id="加法-plus" tabindex="-1"><a class="header-anchor" href="#加法-plus" aria-hidden="true">#</a> 加法 plus</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.plus(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
<span class="token number">0.1</span> <span class="token operator">+</span> <span class="token number">0.2</span>                       <span class="token comment">// 0.30000000000000004</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> x<span class="token punctuation">.</span><span class="token function">plus</span><span class="token punctuation">(</span><span class="token number">0.2</span><span class="token punctuation">)</span>                 <span class="token comment">// '0.3'</span>
<span class="token function">BigNumber</span><span class="token punctuation">(</span><span class="token number">0.7</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">plus</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">plus</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span>  <span class="token comment">// '1'</span>
x<span class="token punctuation">.</span><span class="token function">plus</span><span class="token punctuation">(</span><span class="token string">'0.1'</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>                <span class="token comment">// '0.225'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="减法-minus" tabindex="-1"><a class="header-anchor" href="#减法-minus" aria-hidden="true">#</a> 减法 minus</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.minus(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
<span class="token number">0.3</span> <span class="token operator">-</span> <span class="token number">0.1</span>                       <span class="token comment">// 0.19999999999999998</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">minus</span><span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>                    <span class="token comment">// '0.2'</span>
x<span class="token punctuation">.</span><span class="token function">minus</span><span class="token punctuation">(</span><span class="token number">0.6</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>                <span class="token comment">// '0'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="乘法-multipliedby" tabindex="-1"><a class="header-anchor" href="#乘法-multipliedby" aria-hidden="true">#</a> 乘法 multipliedBy</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.times(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
<span class="token number">0.6</span> <span class="token operator">*</span> <span class="token number">3</span>                         <span class="token comment">// 1.7999999999999998</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">0.6</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> x<span class="token punctuation">.</span><span class="token function">multipliedBy</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>           <span class="token comment">// '1.8'</span>
<span class="token function">BigNumber</span><span class="token punctuation">(</span><span class="token string">'7e+500'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">times</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span>    <span class="token comment">// '1.26e+501'</span>
x<span class="token punctuation">.</span><span class="token function">multipliedBy</span><span class="token punctuation">(</span><span class="token string">'-a'</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>        <span class="token comment">// '-6'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="除法-dividedby" tabindex="-1"><a class="header-anchor" href="#除法-dividedby" aria-hidden="true">#</a> 除法 dividedBy</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.div(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">355</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">113</span><span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">dividedBy</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span>                  <span class="token comment">// '3.14159292035398230088'</span>
x<span class="token punctuation">.</span><span class="token function">div</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>                        <span class="token comment">// '71'</span>
x<span class="token punctuation">.</span><span class="token function">div</span><span class="token punctuation">(</span><span class="token number">47</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>                   <span class="token comment">// '5'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="除法-取整-dividedtointegerby" tabindex="-1"><a class="header-anchor" href="#除法-取整-dividedtointegerby" aria-hidden="true">#</a> 除法-取整 dividedToIntegerBy</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.idiv(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">dividedToIntegerBy</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span>         <span class="token comment">// '1'</span>
x<span class="token punctuation">.</span><span class="token function">idiv</span><span class="token punctuation">(</span><span class="token number">0.7</span><span class="token punctuation">)</span>                     <span class="token comment">// '7'</span>
x<span class="token punctuation">.</span><span class="token function">idiv</span><span class="token punctuation">(</span><span class="token string">'0.f'</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span>               <span class="token comment">// '5'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="除法-取余-modulo" tabindex="-1"><a class="header-anchor" href="#除法-取余-modulo" aria-hidden="true">#</a> 除法-取余 modulo</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.mod(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
<span class="token number">1</span> <span class="token operator">%</span> <span class="token number">0.9</span>                         <span class="token comment">// 0.09999999999999998</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">modulo</span><span class="token punctuation">(</span><span class="token number">0.9</span><span class="token punctuation">)</span>                   <span class="token comment">// '0.1'</span>
y <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">33</span><span class="token punctuation">)</span>
y<span class="token punctuation">.</span><span class="token function">mod</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">)</span>                  <span class="token comment">// '3'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="指数运算-exponentiatedby" tabindex="-1"><a class="header-anchor" href="#指数运算-exponentiatedby" aria-hidden="true">#</a> 指数运算 exponentiatedBy</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.pow(n [, base])</td>
<td>BigNumber</td>
<td><strong>n (必需)</strong>: number/string/BigNumber 参与计算的数字<br><strong>base</strong>: number 进制 (默认为十进制)</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">0.7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>                <span class="token comment">// 0.48999999999999994</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">0.7</span><span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">exponentiatedBy</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>            <span class="token comment">// '0.49'</span>
<span class="token function">BigNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>            <span class="token comment">// '0.11111111111111111111'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="开平方-squareroot" tabindex="-1"><a class="header-anchor" href="#开平方-squareroot" aria-hidden="true">#</a> 开平方 squareRoot</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.sqrt()</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">0.7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>                <span class="token comment">// 0.48999999999999994</span>
x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span><span class="token number">0.7</span><span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">exponentiatedBy</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>            <span class="token comment">// '0.49'</span>
<span class="token function">BigNumber</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>            <span class="token comment">// '0.11111111111111111111'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="四舍五入-tofixed" tabindex="-1"><a class="header-anchor" href="#四舍五入-tofixed" aria-hidden="true">#</a> 四舍五入 toFixed</h3>
<table>
<thead>
<tr>
<th>方法名</th>
<th>返回值</th>
<th>参数值</th>
</tr>
</thead>
<tbody>
<tr>
<td>.toFixed([dp [, rm]])</td>
<td></td>
<td><strong>dp (必需)</strong>: number 参与计算的数字<br><strong>rm</strong>: number</td>
</tr>
</tbody>
</table>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code><span class="token comment">// 举例</span>
x <span class="token operator">=</span> <span class="token number">3.456</span>
y <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigNumber</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
x<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token punctuation">)</span>                     <span class="token comment">// '3'</span>
y<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token punctuation">)</span>                     <span class="token comment">// '3.456'</span>
y<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>                    <span class="token comment">// '3'</span>
x<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>                    <span class="token comment">// '3.46'</span>
y<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>                    <span class="token comment">// '3.46'</span>
y<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>                 <span class="token comment">// '3.45'  (ROUND_DOWN)</span>
x<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>                    <span class="token comment">// '3.45600'</span>
y<span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>                    <span class="token comment">// '3.45600'</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


